document.addEventListener('DOMContentLoaded', function() {
  // 移动端菜单切换
  const menuToggle = document.querySelector('.menu-toggle');
  const navLinks = document.querySelector('.nav-links');
  
  if (menuToggle) {
    menuToggle.addEventListener('click', function() {
      navLinks.classList.toggle('active');
    });
  }
  
  // 打印模式切换
  const printToggle = document.querySelector('.print-toggle');
  const body = document.body;
  
  if (printToggle) {
    printToggle.addEventListener('click', function() {
      body.classList.toggle('print-mode');
      
      if (body.classList.contains('print-mode')) {
        printToggle.innerHTML = '<i class="fas fa-tv"></i> 返回正常视图';
      } else {
        printToggle.innerHTML = '<i class="fas fa-print"></i> 切换打印视图';
      }
    });
  }
  
  // 日期选择和天气查询
  const dateForm = document.getElementById('dateForm');
  const weatherResult = document.querySelector('.weather-result');
  const weatherCards = document.querySelector('.weather-cards');
  
  if (dateForm) {
    dateForm.addEventListener('submit', function(e) {
      e.preventDefault();
      
      const startDate = new Date(document.getElementById('startDate').value);
      const endDate = new Date(document.getElementById('endDate').value);
      
      if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
        alert('请选择有效的日期');
        return;
      }
      
      if (endDate < startDate) {
        alert('结束日期不能早于开始日期');
        return;
      }
      
      const diffTime = Math.abs(endDate - startDate);
      const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + 1;
      
      if (diffDays > 5) {
        alert('行程时间不能超过5天');
        return;
      }
      
      // 模拟天气数据
      const weatherData = generateWeatherData(startDate, diffDays);
      displayWeatherData(weatherData);
      
      weatherResult.classList.add('active');
    });
  }
  
  // 生成模拟的天气数据
  function generateWeatherData(startDate, days) {
    const weatherTypes = ['晴', '多云', '阴', '小雨', '中雨'];
    const weatherIcons = ['sun', 'cloud', 'cloud-sun', 'cloud-rain', 'cloud-showers-heavy'];
    const data = [];
    
    for (let i = 0; i < days; i++) {
      const currentDate = new Date(startDate);
      currentDate.setDate(startDate.getDate() + i);
      
      const weatherIndex = Math.floor(Math.random() * weatherTypes.length);
      const dayTemp = Math.floor(Math.random() * 10) + 20; // 20-30度
      const nightTemp = dayTemp - Math.floor(Math.random() * 5) - 5; // 比日间温度低5-10度
      
      data.push({
        date: currentDate.toLocaleDateString('zh-CN'),
        weather: weatherTypes[weatherIndex],
        icon: weatherIcons[weatherIndex],
        dayTemp: dayTemp,
        nightTemp: nightTemp
      });
    }
    
    return data;
  }
  
  // 显示天气数据
  function displayWeatherData(data) {
    weatherCards.innerHTML = '';
    
    data.forEach(day => {
      const card = document.createElement('div');
      card.className = 'weather-card';
      
      card.innerHTML = `
        <div class="weather-date">${day.date}</div>
        <div class="weather-icon"><i class="fas fa-${day.icon}"></i> ${day.weather}</div>
        <div class="weather-temp">日间: ${day.dayTemp}°C</div>
        <div class="weather-temp">夜间: ${day.nightTemp}°C</div>
      `;
      
      weatherCards.appendChild(card);
    });
  }
  
  // 设置日期选择器的默认值和最小值
  const startDateInput = document.getElementById('startDate');
  const endDateInput = document.getElementById('endDate');
  
  if (startDateInput && endDateInput) {
    const today = new Date();
    const year = today.getFullYear();
    let month = today.getMonth() + 1;
    let day = today.getDate();
    
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    
    const formattedToday = `${year}-${month}-${day}`;
    
    startDateInput.min = formattedToday;
    endDateInput.min = formattedToday;
    
    // 设置默认为五一假期（假设为5月1日开始的5天）
    const mayFirst = `${year}-05-01`;
    const mayFifth = `${year}-05-05`;
    
    startDateInput.value = mayFirst;
    endDateInput.value = mayFifth;
  }
}); 